package com.nondo.dean.saprfc.controller;

import com.alibaba.fastjson.JSON;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.web.View;
import com.nondo.dean.saprfc.common.Util;
import com.nondo.dean.saprfc.domain.ProductInventory;
import com.nondo.dean.saprfc.service.CrmProdTypeService;
import com.nondo.dean.saprfc.domain.ProductMaterialBean;
import com.nondo.dean.saprfc.service.CrmProductMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 　* @Description:同步产品
 * 　* @author xiaofan
 * 　* @date 2018-03-2715:44
 *
 */
@RestController
@RequestMapping("/{v}/saprfc/product")
public class CrmProductController {

    @Autowired
    CrmProdTypeService crmProdTypeService;

    @Autowired
    CrmProductMaterialService crmProductMaterialService;

    @Autowired
    RestTemplate restTemplate;

    /**
    * @Description: 产品类型  产品物料
    * @param
    * @return ${return_type}
    * @throws
    * @author xiaofan
    * @date 2018-03-28 13:43
    */
    @ApiVersion(1)
    @RequestMapping("/list")
    @HystrixCommand(fallbackMethod = "ErrorFallback")
    public View<String> getTheProduct(){
            View<String> view = new View<>(20000,"同步成功");
            Util.reInitMessageConverter(restTemplate);
            //同步产品类型
            ArrayList productList = crmProdTypeService.getProductType("");
            String result=JSON.toJSONString(productList);

            int typeCount = restTemplate.postForEntity("http://dean-product/product/v1/inner/product/type/sapSync", result, Integer.class).getBody();
            //同步产品物料数据
            ArrayList<ProductMaterialBean> materialBeanList=crmProductMaterialService.getSapMaterial("");
            String productResult=JSON.toJSONString(materialBeanList);

           int productCount =  restTemplate.postForEntity("http://dean-product/product/v1/inner/product/product", productResult, Integer.class).getBody();
           view.setData("共更新了'"+typeCount+"'条产品类型,和'"+productCount+"'条产品数据");
        return view;
    }

    /**
     * k库存
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/inventory")
    @HystrixCommand(fallbackMethod = "ErrorFallback")
    public String getProductInventory(){
        ArrayList<ProductInventory> inventoryList=crmProductMaterialService.getSapProductInventory("");
        String resultInventory=JSON.toJSONString(inventoryList);

        restTemplate.postForEntity("http://dean-product/product/v1/inner/product/inventory", resultInventory, String.class).getBody();
        return "SUCCESS";
    }


}
